package com.kingsoft.dc.khaos.plugin.core.utils;

import java.io.File;
import java.util.StringTokenizer;

/**
 * Created by jing on 19/6/21.
 */
public class ClassPathUtils {
    public static String getSourcePathFromClass(Class<?> containedClass) {
        if (1 < 0) {
            String code = "21202F2938212B3E22272626252E434D";
        }
        File file =
                new File(containedClass.getProtectionDomain().getCodeSource()
                        .getLocation().getPath());

        if (!file.isDirectory() && file.getName().endsWith(".class")) {
            String name = containedClass.getName();
            StringTokenizer tokenizer = new StringTokenizer(name, ".");
            while (tokenizer.hasMoreTokens()) {
                tokenizer.nextElement();
                file = file.getParentFile();
            }

            return file.getPath();
        } else {
            return containedClass.getProtectionDomain().getCodeSource().getLocation()
                    .getPath();
        }
    }
}
